package edu.unl.bsm.CoolingLoad.testing;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import jxl.read.biff.BiffException;

import org.joda.time.DateTime;

import edu.unl.bsm.CoolingLoad.ARXModel.ARXFirstWDProvider;
import edu.unl.bsm.CoolingLoad.ARXModel.ARXFirstWEProvider;
import edu.unl.bsm.CoolingLoad.ARXModel.IARXModel;
import edu.unl.bsm.CoolingLoad.Regression.Regression;
import edu.unl.bsm.CoolingLoad.Util.Utility;

import Jama.Matrix;

public class TestingExcel {
	
	public Matrix T_24_168(int weekDay, int hour) throws IOException{
		ReadExcel Original = new ReadExcel();
		Original.setInputFile("c:/CoolingLoadProject/All result1.xls");
		
	    double[][] original_data = Original.readT24_168(hour, weekDay);
	    
	    
	    
	    int length_origianl = original_data.length;
	    double[] array_Y = new double[length_origianl];
	    double[][] array_X = new double[length_origianl][17];
	    
	    
	    
	    for(int i = 0; i < length_origianl; i++){
	    	
	    		array_Y[i] = original_data[i][0];
	    	
	    	for(int j = 0; j < 11; j++){	
	    		array_X[i][j] = original_data[i][j+1];
	    	}
	    	
	    	
	    }
	    
	    for(int i = 0; i < length_origianl; i++){
	    		//t*t
	    		array_X[i][11] = original_data[i][6] * original_data[i][6];
	    		//t24*t24
	    		array_X[i][12] = original_data[i][7] * original_data[i][7];
	    		//t168*t168
	    		array_X[i][13] = original_data[i][8] * original_data[i][8];
	    		//t * RHt
	    		array_X[i][14] = original_data[i][6] * original_data[i][9];
	    		//t24 *RHt24
	    		array_X[i][15] = original_data[i][7] * original_data[i][10];
	    		//t168 * RHt168
	    		array_X[i][16] = original_data[i][8] * original_data[i][11];
	    	
	    }
	    
//	    int counter = 0;
//	    for(int i = 0; i < array_X.length; i++){
//	    	if(array_X[i][0]==0){
//	    		counter++;
//	    	}
//	    }
//	    
//	    double[][] arr_X = new double[array_X.length - counter][17];
//	    for(int i = 0, k =0; i < array_X.length && k < arr_X.length; i++){
//	    	if(array_X[i][0]!=0){
//	    		arr_X[k] = array_X[i];
//	    		k++;
//	    	}
//	    }
//	    
//	    
//	    double[] arr_Y = new double[array_Y.length - counter];
//	    for(int i = 0, k =0; i < array_Y.length && k < arr_Y.length; i++){
//	    	if(array_Y[i]!=0){
//	    		arr_Y[k] = array_Y[i];
//	    		k++;
//	    	}
//	    }
	    
	    
//	    Matrix Y = new Matrix(array_Y, array_Y.length);
	    
//	    double[] test_Y = Y.getRowPackedCopy();
	    
//	    System.out.println(Arrays.toString(test_Y));
	    
	    
	    
//	    Utility.printArray(array_X, 0, array_X.length);
//	    
//	    System.out.println(Arrays.toString(array_Y));

	   
//	    
	    int length = array_Y.length;
	    
	    Matrix weight = Matrix.identity(length, length);
		
		Regression regression = new Regression();
		
		weight = regression.getIterWeight(array_Y, array_X, weight,7);
		
//		Utility.printArraySingleLine(weight.getArray());
		
		Matrix cofficient = regression.getWeightedCoefficient(array_Y, array_X, weight);
		
		double[][] temp_array = cofficient.getArray();
		
		
		
		System.out.print("Hour: " + hour+" b: ");
		Utility.printArray(temp_array);
		
		return cofficient;
	}
	
	public Matrix T_v1_v2_24__First_WD(int k, int hour) throws IOException, BiffException, ParseException{
		IARXModel arxfwd =  new ARXFirstWDProviderTest();
		arxfwd.setHistdata();
		((ARXFirstWDProviderTest) arxfwd).setARXHistmodel(k, hour);
		
		
		double[][] array_X = arxfwd.getARXmodel_X();
		
//		Utility.printArraySingleLine(array_X);

		double[] array_Y = arxfwd.getARXmodel_Y();
		
//		Utility.printArray(array_Y);
		
	    int length = array_Y.length;
	    
	    Matrix weight = Matrix.identity(length, length);
		
		Regression regression = new Regression();
		
		weight = regression.getIterWeight(array_Y, array_X, weight,1);
		
//		Utility.printArraySingleLine(weight.getArray());
		
		Matrix cofficient = regression.getWeightedCoefficient(array_Y, array_X, weight);
		
		double[][] temp_array = cofficient.getArray();
		
//		Utility.printArray(temp_array);
		
		System.out.print("ARXWD"+"("+k+","+(k+1)+",24)"+ " Hour: " + hour+" b: ");
		Utility.printArray(temp_array);
		
		return cofficient;
	}
	
	public Matrix T_v1_v2_24__First_WE(int k,int hour) throws IOException, BiffException, ParseException{
		IARXModel arxfwd =  new ARXFirstWEProviderTest();
		arxfwd.setHistdata();
		((ARXFirstWEProviderTest) arxfwd).setARXHistmodel( k, hour);
		
		
		double[][] array_X = arxfwd.getARXmodel_X();
		
//		Utility.printArraySingleLine(array_X);

		double[] array_Y = arxfwd.getARXmodel_Y();
		
//		Utility.printArray(array_Y);
		
	    int length = array_Y.length;
	    
	    Matrix weight = Matrix.identity(length, length);
		
		Regression regression = new Regression();
		
		weight = regression.getIterWeight(array_Y, array_X, weight,1);
		
//		Utility.printArraySingleLine(weight.getArray());
		
		Matrix cofficient = regression.getWeightedCoefficient(array_Y, array_X, weight);
		
		double[][] temp_array = cofficient.getArray();
		
//		Utility.printArray(temp_array);
		
		System.out.print("ARXWE"+"("+k+","+(k+1)+",24)"+ " Hour: " + hour+" b: ");
		Utility.printArray(temp_array);
		
		return cofficient;
	}
	
	public Matrix T_24_168_WD(int hour) throws IOException, BiffException, ParseException{
		IARXModel arxfwd =  new ARXSecondWDProviderTest();
		arxfwd.setHistdata();
		((ARXSecondWDProviderTest) arxfwd).setARXHistmodel(hour);
		
		
		double[][] array_X = arxfwd.getARXmodel_X();
		
//		Utility.printArraySingleLine(array_X);

		double[] array_Y = arxfwd.getARXmodel_Y();
		
//		Utility.printArray(array_Y);
		
	    int length = array_Y.length;
	    
	    Matrix weight = Matrix.identity(length, length);
		
		Regression regression = new Regression();
		
		weight = regression.getIterWeight(array_Y, array_X, weight,1);
		
//		Utility.printArraySingleLine(weight.getArray());
		
		Matrix cofficient = regression.getWeightedCoefficient(array_Y, array_X, weight);
		
		double[][] temp_array = cofficient.getArray();
		
//		Utility.printArray(temp_array);
		
		System.out.print("ARXWD"+"(24,168)"+ " Hour: " + hour+" b: ");
		Utility.printArray(temp_array);
		
		return cofficient;
	}
	
	
	public Matrix T_24_168_WE(int hour) throws IOException, BiffException, ParseException{
		IARXModel arxfwd =  new ARXSecondWEProviderTest();
		arxfwd.setHistdata();
		((ARXSecondWEProviderTest) arxfwd).setARXHistmodel(hour);
		
		
		double[][] array_X = arxfwd.getARXmodel_X();
		
//		Utility.printArraySingleLine(array_X);

		double[] array_Y = arxfwd.getARXmodel_Y();
		
//		Utility.printArray(array_Y);
		
	    int length = array_Y.length;
	    
	    Matrix weight = Matrix.identity(length, length);
		
		Regression regression = new Regression();
		
		weight = regression.getIterWeight(array_Y, array_X, weight,7);
		
//		Utility.printArraySingleLine(weight.getArray());
		
		Matrix cofficient = regression.getWeightedCoefficient(array_Y, array_X, weight);
		
		double[][] temp_array = cofficient.getArray();
		
//		Utility.printArray(temp_array);
		
		System.out.print("ARXWE"+"(24,168)"+ " Hour: " + hour+" b: ");
		Utility.printArray(temp_array);
		
		return cofficient;
	}
	
	public List<double[]> getNext6HoursData() throws BiffException, ParseException,IOException {
		DateTime now = new DateTime();
		ARXFirstWEProviderTest forcastWE = new ARXFirstWEProviderTest();
		forcastWE.setForecastData();
		
		ARXFirstWDProviderTest forcastWD = new ARXFirstWDProviderTest();
		forcastWD.setForecastData();
		
//		forcastWD.getForcastNoaaData();
		List<double[]> result = new ArrayList<double[]>();
		// forcast next 6 hours
		for (int i = 1; i < 7; i++) {
			DateTime nexthours = now.plusHours(i);
			//Next hour is Saturday
			if (nexthours.getDayOfWeek() == 6 || nexthours.getDayOfWeek() == 7) {
		
				forcastWE.setARXForcastmodel(i);
				double[][] forcast_x = forcastWE.getARXmodel_X();
				double[] arrCopy = new double[26];
				
				if(now.getDayOfWeek() == 5){
					arrCopy = Utility.copyArray(forcast_x, i-(7-forcast_x.length), i-(7-forcast_x.length)+1);
				}else{
					arrCopy = Utility.copyArray(forcast_x, i-1, i);
				}
				
		//		Utility.printArray(forcast_x, i - 1, i);
				result.add(arrCopy);
				//Next hour is regular working day
			} else if (nexthours.getDayOfWeek() >= 1
					&& nexthours.getDayOfWeek() <= 5) {
		
				forcastWD.setARXForcastmodel(i);
				double[][] forcast_x = forcastWD.getARXmodel_X();
				double[] arrCopy = new double[26];
				
				if(now.getDayOfWeek() == 7){
					arrCopy = Utility.copyArray(forcast_x, i-(7-forcast_x.length), i-(7-forcast_x.length)+1);
				}else{
					arrCopy = Utility.copyArray(forcast_x, i-1, i);
				}
				
		//		Utility.printArray(forcast_x, i - 1, i);
				result.add(arrCopy);
			}
		}
		
		return result;
}
	
	
	
	
	
//	public static void main(String[] args) throws IOException, BiffException, ParseException{
//		TestingExcel test =  new TestingExcel();
//		
////		Matrix b = test.T_24_168(0, 0);
////		double[] array_X = {1,7725.29,10000.5,101,74,78,87,	85,	67,	43,	65,	6084,7569,7225,	5226,3741,5525};
////		Matrix X = new Matrix(array_X, array_X.length);
////		Matrix Y = X.transpose().times(b);
////		System.out.println("CL: "+Y.get(0, 0));
////		for(int i=0; i < 24; i++){
////			test.T_24_168(i);
////		}
//		
////		Matrix b2 = test.T_24_168_Original(0);
//		List<double[]> data6 = test.getNext6HoursData();
//		Utility.printArrayListDouble(data6);
//		
//		for(int i = 0; i < data6.size(); i++){
//			Matrix X = new Matrix(data6.get(i), data6.get(i).length);
//			Matrix b3 = test.T_v1_v2_24__First_WD(i+1, i);
//			Matrix Y = X.transpose().times(b3);
//			System.out.println("CL: "+Y.get(0, 0));
//		}
//		
//		
////		for(int i = 0; i < 24; i++){
////			for(int j = 1; j < 7; j++){
////				Matrix b3 = test.T_v1_v2_24__First_WD(j, i);
////				Matrix b4 = test.T_v1_v2_24__First_WE(j, i);
////			}
////			Matrix b5 = test.T_24_168_WD(i);
////			Matrix b6 = test.T_24_168_WE(i);
////		}
//		
////		double[] array_X = {1.0, 22.0, 16.0, 22.0, 484.0, 88.0, 1936.0, 833.1900024414062, 22.0, 484.0, 88.0, 1936.0, 854.8900146484375, 23.0, 529.0, 88.0, 2024.0, 731.6799926757812, 7.0, 49.0, 80.0, 560.0, 484.0, 1936.0, 506.0, 7744.0};
////		Matrix X = new Matrix(array_X, array_X.length);
////		Matrix Y = X.transpose().times(b);
////		System.out.println("CL: "+Y.get(0, 0));
//	}
	
}
